package org.bikelab.se3.index;

import org.apache.hadoop.conf.Configuration;

public class Se3IndexWriterFactory {
	@SuppressWarnings("unchecked")
	public static Se3IndexWriter[] getIndexWriters(Configuration conf) {
		final String[] classes = conf.getStrings("indexer.writer.classes");
		if(classes == null) {
			throw new RuntimeException("No IndexWriter classes registered.");
		}
		final Se3IndexWriter[] writers = new Se3IndexWriter[classes.length];
		for (int i = 0; i < classes.length; i++) {
			final String clazz = classes[i];
			try {
				final Class<Se3IndexWriter> implClass = (Class<Se3IndexWriter>) Class
						.forName(clazz);
				writers[i] = implClass.newInstance();
			} catch (final Exception e) {
				throw new RuntimeException("Couldn't create " + clazz, e);
			}
		}
		return writers;
	}

	public static void addClassToConf(Configuration conf,
			Class<? extends Se3IndexWriter> clazz) {
		final String classes = conf.get("indexer.writer.classes");
		final String newClass = clazz.getName();

		if (classes == null) {
			conf.set("indexer.writer.classes", newClass);
		} else {
			conf.set("indexer.writer.classes", classes + "," + newClass);
		}

	}
	
	public static void addClassToConf(Configuration conf,
			String newClass) {
		final String classes = conf.get("indexer.writer.classes");

		if (classes == null) {
			conf.set("indexer.writer.classes", newClass);
		} else {
			conf.set("indexer.writer.classes", classes + "," + newClass);
		}
	}
}
